package com.firmboy.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * jdk动态代理
 *
 * @author firmboy
 * @create 2018-05-06 下午7:28
 **/
public class MyInvocationHandler implements InvocationHandler{

    //目标对象
    private Object target;

    public MyInvocationHandler(Object target){
        super();
        this.target = target;
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("----------before--------------");

        Object result = method.invoke(target, args);

        System.out.println("----------after---------------");

        return result;
    }

    public Object getProxy(){

        return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),target.getClass().getInterfaces(),this);

    }
}
